| Conditions | 1 |
| Paths | 15 |
| Total Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 33 | $.fn.symphonySelectable = function(options) { |
||
| 34 | var objects = this, |
||
| 35 | settings = { |
||
| 36 | items: 'tbody tr:has(input)', |
||
| 37 | ignore: 'a', |
||
| 38 | mode: 'single' |
||
| 39 | }; |
||
| 40 | |||
| 41 | $.extend(settings, options); |
||
| 42 | |||
| 43 | /*------------------------------------------------------------------------- |
||
| 44 | Events |
||
| 45 | -------------------------------------------------------------------------*/ |
||
| 46 | |||
| 47 | // Select |
||
| 48 | objects.on('click.selectable', settings.items, function select(event) { |
||
| 49 | var item = $(this), |
||
| 50 | items = item.siblings().addBack(), |
||
| 51 | object = $(event.liveFired), |
||
| 52 | target = $(event.target), |
||
| 53 | selection, deselection, first, last; |
||
| 54 | |||
| 55 | // Ignored elements |
||
| 56 | if(target.is(settings.ignore)) { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Remove text ranges |
||
| 61 | if(window.getSelection) { |
||
| 62 | window.getSelection().removeAllRanges(); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Range selection |
||
| 66 | if((event.shiftKey) && items.filter('.selected').length > 0 && !object.is('.single')) { |
||
| 67 | |||
| 68 | // Select upwards |
||
| 69 | if(item.prevAll().filter('.selected').length > 0) { |
||
| 70 | first = items.filter('.selected:first').index(); |
||
| 71 | last = item.index() + 1; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Select downwards |
||
| 75 | else { |
||
| 76 | first = item.index(); |
||
| 77 | last = items.filter('.selected:last').index() + 1; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Get selection |
||
| 81 | selection = items.slice(first, last); |
||
| 82 | |||
| 83 | // Deselect items outside the selection range |
||
| 84 | deselection = items.filter('.selected').not(selection).removeClass('selected').trigger('deselect.selectable'); |
||
| 85 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 86 | |||
| 87 | // Select range |
||
| 88 | selection.addClass('selected').trigger('select.selectable'); |
||
| 89 | selection.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
|
|
|||
| 90 | } |
||
| 91 | |||
| 92 | // Single selection |
||
| 93 | else { |
||
| 94 | |||
| 95 | // Press meta or ctrl key to adjust current range, otherwise the selection will be removed |
||
| 96 | if((!event.metaKey && !event.ctrlKey && settings.mode != 'additive' && !target.is('input')) || object.is('.single')) { |
||
| 97 | deselection = items.not(item).filter('.selected').removeClass('selected').trigger('deselect.selectable'); |
||
| 98 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Toggle selection |
||
| 102 | if(item.is('.selected')) { |
||
| 103 | item.removeClass('selected').trigger('deselect.selectable'); |
||
| 104 | item.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 105 | } |
||
| 106 | else { |
||
| 107 | item.addClass('selected').trigger('select.selectable'); |
||
| 108 | item.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | }); |
||
| 113 | |||
| 114 | // Remove all selections by doubleclicking the body |
||
| 115 | $('body').on('dblclick.selectable', function removeAllSelection() { |
||
| 116 | objects.find(settings.items).removeClass('selected').trigger('deselect.selectable'); |
||
| 117 | }); |
||
| 118 | |||
| 119 | /*------------------------------------------------------------------------- |
||
| 120 | Initialisation |
||
| 121 | -------------------------------------------------------------------------*/ |
||
| 122 | |||
| 123 | // Make selectable |
||
| 124 | objects.addClass('selectable'); |
||
| 125 | |||
| 126 | /*-----------------------------------------------------------------------*/ |
||
| 127 | |||
| 128 | return objects; |
||
| 129 | }; |
||
| 130 | |||
| 132 |